--[[
	Ikarus Unit-Frames Addon	
	released under GNU General Public License version 3 (GPLv3) 
	by McGaerty & Sl4ker
	
	contact: mcgaerty@ikarus-systems.net rom@sl4ker.de	
	download: http://rom.curse.com/downloads/rom-addons/details/ikarus-unitframes.aspx
	
	special thanks to Qwox and novayuna for inspiration and stable basics
]]



do

ikarus.config.defaults.playercastbar = {
	["GENERAL"] = {
		["VISIBLE"] = true,
		["FRAME"] = "Ikarus_CastBarFrame",
		["DEFAULT_FRAME"] = "CastingBarFrame",
		["HIDE_INACTIVE"] = true,
		["USE_DEFAULT_FRAME"] = true,
		["OFFSET_X"] = 500,
		["OFFSET_Y"] = 450,
		["WIDTH"] = 200,
		["HEIGHT"] = 18,
		["ALPHA"] = 1.0,
		["FRAME_LOCKED"] = false,
		["SCALE"] = 1.0
	},
	
	["DOCKING"] = {
		["ENABLE"] = false,
		["TARGET"] = "PLAYER",
		["OFFSET_X"] = 0,
		["OFFSET_Y"] = 20,
		["ALIGNMENT"] = "BOTTOM"
	},
	
	["PROGRESSBAR"] = {
		["VISIBLE"] = true,
		["TEXTURE"] = "Healbot",
		["FRAME"] = "Ikarus_CastBarFrameProgressBar",
		["FADEOUT_TIME"] = 1.0,
		["HOLD_TIME"] = 1.0,
		["FAIL_COLOR"] = { r = 1.0, g = 0.0, b = 0.0 },
		["FOREGROUND_COLOR"] = { r = 0.1, g = 0.7, b = 0.7 },
		["BACKGROUND_COLOR"] = { r = 0.2, g = 0.4, b = 0.65, a = 0.5},
		
		["LEFTTEXT_VISIBLE"] = true,
		["LEFTTEXT_FRAME"] = "Ikarus_CastBarFrameProgressBarLeftText",
		["LEFTTEXT_OFFSET_X"] = 100,
		["LEFTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["LEFTTEXT_FONT"] = "Default",
		["LEFTTEXT_FONTSIZE"] = 10,
		["LEFTTEXT_COLORIZER"] = "NONE",
		["LEFTTEXT_CONTENT"] = "[SPELL]",
		
		["RIGHTTEXT_VISIBLE"] = false,
		["RIGHTTEXT_FRAME"] = "Ikarus_CastBarFrameProgressBarRightText",
		["RIGHTTEXT_OFFSET_X"] = 1,
		["RIGHTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["RIGHTTEXT_FONT"] = "Default",
		["RIGHTTEXT_FONTSIZE"] = 10,
		["RIGHTTEXT_COLORIZER"] = "NONE",
		["RIGHTTEXT_CONTENT"] = "",
		
		["OUTSIDE_LEFTTEXT_VISIBLE"] = false,
		["OUTSIDE_LEFTTEXT_FRAME"] = "Ikarus_CastBarFrameProgressBarOutsideLeftText",
		["OUTSIDE_LEFTTEXT_OFFSET_X"] = 1,
		["OUTSIDE_LEFTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["OUTSIDE_LEFTTEXT_FONT"] = "Default",
		["OUTSIDE_LEFTTEXT_FONTSIZE"] = 10,
		["OUTSIDE_LEFTTEX_COLORIZER"] = "NONE",
		["OUTSIDE_LEFTTEXT_CONTENT"] = "",
		
		["OUTSIDE_RIGHTTEXT_VISIBLE"] = false,
		["OUTSIDE_RIGHTTEXT_FRAME"] = "Ikarus_CastBarFrameProgressBarOutsideRightText",
		["OUTSIDE_RIGHTTEXT_OFFSET_X"] = 1,
		["OUTSIDE_RIGHTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["OUTSIDE_RIGHTTEXT_FONT"] = "Default",
		["OUTSIDE_RIGHTTEXT_FONTSIZE"] = 10,
		["OUTSIDE_RIGHTTEXT_COLORIZER"] = "NONE",
		["OUTSIDE_RIGHTTEXT_CONTENT"] = ""
	},
	
	["LABELS"] = {
		["TOPTEXT_VISIBLE"] = true,
		["TOPTEXT_FRAME"] = "Ikarus_CastBarFrameOutsideTopText",
		["TOPTEXT_OFFSET_X"] = 20,
		["TOPTEXT_OFFSET_Y"] = 1,
		["TOPTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["TOPTEXT_FONT"] = "Default",
		["TOPTEXT_FONTSIZE"] = 10,
		["TOPTEXT_COLORIZER"] = "NONE",
		["TOPTEXT_CONTENT"] = "[SPELL_NAME]",
		
		["RIGHTTEXT_VISIBLE"] = false,
		["RIGHTTEXT_FRAME"] = "Ikarus_CastBarFrameOutsideRightText",
		["RIGHTTEXT_OFFSET_X"] = 1,
		["RIGHTTEXT_OFFSET_Y"] = 1,
		["RIGHTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["RIGHTTEXT_FONT"] = "Default",
		["RIGHTTEXT_FONTSIZE"] = 10,
		["RIGHTTEXT_COLORIZER"] = "NONE",
		["RIGHTTEXT_CONTENT"] = "",
		
		["BOTTOMTEXT_VISIBLE"] = false,
		["BOTTOMTEXT_FRAME"] = "Ikarus_CastBarFrameOutsideBottomText",
		["BOTTOMTEXT_OFFSET_X"] = 1,
		["BOTTOMTEXT_OFFSET_Y"] = 1,
		["BOTTOMTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["BOTTOMTEXT_FONT"] = "Default",
		["BOTTOMTEXT_FONTSIZE"] = 10,
		["BOTTOMTEXT_COLORIZER"] = "NONE",
		["BOTTOMTEXT_CONTENT"] = "",
		
		["LEFTTEXT_VISIBLE"] = false,
		["LEFTTEXT_FRAME"] = "Ikarus_CastBarFrameOutsideLeftText",
		["LEFTTEXT_OFFSET_X"] = 1,
		["LEFTTEXT_OFFSET_Y"] = 1,
		["LEFTTEXT_TEXTCOLOR"] = { r = 1.0, g = 1.0, b = 1.0 },
		["LEFTTEXT_FONT"] = "Default",
		["LEFTTEXT_FONTSIZE"] = 10,
		["LEFTTEXT_COLORIZER"] = "NONE",
		["LEFTTEXT_CONTENT"] = ""
	}
};

table.insert(ikarus.config.interface, 
	{
		"playercastbar",
		
		--  General
		
		{
			"GENERAL", 
	        {"VISIBLE", nil,nil,"CHECKBOX", true},
	        {"USE_DEFAULT_FRAME", nil, nil,"CHECKBOX", true},
			{"DUMMY_STATE", nil, nil,"BUTTON", false}, 
			{"HIDE_INACTIVE", nil, nil,"CHECKBOX"},
	        {"OFFSET_X", nil, nil,"SLIDER",false,0,"##interface.MAX_OFFSET_X"},
	        {"OFFSET_Y", nil, nil,"SLIDER",false,0,"##interface.MAX_OFFSET_Y"},
			{"WIDTH", nil, nil,"SLIDER",false,50,300},
			{"HEIGHT", nil, nil,"SLIDER",false,10,80},
	        {"ALPHA", nil, nil,"SLIDER", false, 0.0,1.0, 0.1},
	        {"NUMBER_FORMAT", nil, nil,"COMBOBOX", false, "##interface.NUMBER_FORMATS", true},
	        {"FRAME_LOCKED", nil, nil,"CHECKBOX"},
	        {"SCALE", nil, nil,"SLIDER",false,0.5,2.0}
		},
		
		--  Docking
		
		{
			"DOCKING", 
	        {"ENABLE", nil,nil,"CHECKBOX", false},
	        {"TARGET", nil, nil,"COMBOBOX", false, {"PLAYER","PARTY","TARGET"}, true},
	        {"OFFSET_X", nil, nil,"SLIDER",false,-250,250},
	        {"OFFSET_Y", nil, nil,"SLIDER",false,-250,250},
	        {"ALIGNMENT", nil,nil,"COMBOBOX",false,"##interface.ALIGNMENTS", true},
		},
		
		--  Progress bar
		
		{	
			"PROGRESSBAR",
			"GENERAL",
			{"TEXTURE", nil,nil,"COMBOBOX",true,"##interface.BAR_TEXTURES"},
			{"FADEOUT_TIME", nil, nil,"SLIDER",false,0.0,2.0},
			{"HOLD_TIME", nil, nil,"SLIDER",false,0.0,2.0},
			{"FAIL_COLOR", nil,nil,"COLORPICKER"},
			{"FOREGROUND_COLOR", nil,nil,"COLORPICKER"},
			{"BACKGROUND_COLOR", nil,nil,"COLORPICKER"},
			
			"LEFTTEXT",
			{"LEFTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", true},
			{"LEFTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"LEFTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"LEFTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"LEFTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"LEFTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetWidth"},
			
			"RIGHTTEXT",
			{"RIGHTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", false},
			{"RIGHTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"RIGHTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"RIGHTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"RIGHTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"RIGHTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,"$$Ikarus_CastBarFrame.GetWidth", 0},
			
			"OUTSIDE_LEFTTEXT",
			{"OUTSIDE_LEFTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", false},
			{"OUTSIDE_LEFTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"OUTSIDE_LEFTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"OUTSIDE_LEFTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"OUTSIDE_LEFTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"OUTSIDE_LEFTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,"$$Ikarus_CastBarFrame.GetWidth", 0},
			
			"OUTSIDE_RIGHTTEXT",
			{"OUTSIDE_RIGHTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", false},
			{"OUTSIDE_RIGHTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"OUTSIDE_RIGHTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"OUTSIDE_RIGHTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"OUTSIDE_RIGHTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"OUTSIDE_RIGHTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetWidth"},
		},
		
		--	Labels
		
		{
			"LABELS",
			"TOPTEXT",
			{"TOPTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", true},
			{"TOPTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"TOPTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"TOPTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"TOPTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"TOPTEXT_COLORIZER", "COLORIZER",nil,"COMBOBOX",true,"##interface.COLORIZERS", true},
			{"TOPTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetWidth"},
			{"TOPTEXT_OFFSET_Y", "OFFSET_Y",nil,"SLIDER",false,-100,0},
			"RIGHTTEXT",
			{"RIGHTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", true},
			{"RIGHTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"RIGHTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"RIGHTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"RIGHTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"RIGHTTEXT_COLORIZER", "COLORIZER",nil,"COMBOBOX",true,"##interface.COLORIZERS", true},
			{"RIGHTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,0,100},
			{"RIGHTTEXT_OFFSET_Y", "OFFSET_Y",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetHeight"},
			"BOTTOMTEXT",
			{"BOTTOMTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", true},
			{"BOTTOMTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"BOTTOMTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"BOTTOMTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"BOTTOMTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"BOTTOMTEXT_COLORIZER", "COLORIZER",nil,"COMBOBOX",true,"##interface.COLORIZERS", true},
			{"BOTTOMTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetWidth"},
			{"BOTTOMTEXT_OFFSET_Y", "OFFSET_Y",nil,"SLIDER",false,0,100},
			"LEFTTEXT",
			{"LEFTTEXT_VISIBLE", "VISIBLE",nil,"CHECKBOX", true},
			{"LEFTTEXT_TEXTCOLOR", "TEXT_COLOR",nil,"COLORPICKER"},
			{"LEFTTEXT_FONT", "FONT",nil,"COMBOBOX",true,"##interface.FONTS"},
			{"LEFTTEXT_FONTSIZE", "FONT_SIZE",nil,"SLIDER",false,"##interface.MIN_FONT_SIZE","##interface.MAX_FONT_SIZE"},
			{"LEFTTEXT_CONTENT", "CONTENT",nil,"TEXTFIELD"},
			{"LEFTTEXT_COLORIZER", "COLORIZER",nil,"COMBOBOX",true,"##interface.COLORIZERS", true},
			{"LEFTTEXT_OFFSET_X", "OFFSET_X",nil,"SLIDER",false,-100,0},
			{"LEFTTEXT_OFFSET_Y", "OFFSET_Y",nil,"SLIDER",false,0,"$$Ikarus_CastBarFrame.GetHeight"}
		}
    }
);


--  ***********************************************************************
--  class handling castbar unit frame behavior
ikarus.delegates.CastBarUnitDelegateClass = ikarus:NewClass(ikarus.delegates.BasicUnitDelegateClass);

function ikarus.delegates.CastBarUnitDelegateClass:Init()
	self.unit = "playercastbar";
	self.isCastBar = true;
	self.frame = getglobal(ikarus.config.defaults.playercastbar.GENERAL.FRAME);
	self.defaultFrame = getglobal(ikarus.config.defaults.playercastbar.GENERAL.DEFAULT_FRAME);
	self.eventList = {
		["CASTING_START"]			= ikarus.delegates.CastBarUnitDelegateClass.UpdateAll;
		["CASTING_STOP"] 			= ikarus.delegates.CastBarUnitDelegateClass.UpdateAll;
		["CASTING_FAILED"] 			= ikarus.delegates.CastBarUnitDelegateClass.UpdateAll; 
		["CASTING_DELAYED"] 		= ikarus.delegates.CastBarUnitDelegateClass.UpdateAll
	};
	self:RegisterAllEvents();
end;

function ikarus.delegates.CastBarUnitDelegateClass:RegisterAllEvents()
	self:RegisterEvents(self);
end;

function ikarus.delegates.CastBarUnitDelegateClass:UnregisterAllEvents()
	self:UnregisterEvents(self);
end;

function ikarus.delegates.CastBarUnitDelegateClass:IsUnit()
	return false;
end;

function ikarus.delegates.CastBarUnitDelegateClass:SetState(spell, duration, start, stop, fadeOut, hold, progress)
	self.spell = spell;
	self.duration = duration;
	self.startTime = start;
	self.stopTime = stop;
	self.fadeOut = fadeOut;
	if(spell) then
		self.spentTime = GetTime() - self.startTime;
		self.progress = self.spentTime / (self.stopTime - self.startTime);
	else
		self.spentTime = 0;
		self.progress = progress or 0;
	end;
	if(fadeOut) then
		self.alphaStep = 1/((self:Profile().PROGRESSBAR.FADEOUT_TIME+0.001)*GetFramerate());
	else
		self.frame:SetAlpha(self:Profile().GENERAL.ALPHA);
	end;
	if(hold) then
		self.holdTime = GetTime() + self:Profile().PROGRESSBAR.HOLD_TIME;
	else
		self.holdTime = 0;
	end;

	self:UpdateProgressBar();
	self:UpdateText();
end;

function ikarus.delegates.CastBarUnitDelegateClass:OnEvent(event, ...)        
    if(event == "CASTING_START" and self:Profile().GENERAL.VISIBLE)then
		--  Extremly bad strategy of RoM here.... global vars suck....
		local t = GetTime();
		self.frame:Show();
		getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetColor(unpackCol(self:Profile().PROGRESSBAR.FOREGROUND_COLOR));
		self:SetState(arg1, arg2, t, t + arg2, false, false, 0);
    elseif(event == "CASTING_STOP" and self.frame:IsVisible())then
		self:SetState(nil, nil, nil, nil, true, true, 0);
    elseif(event == "CASTING_FAILED" and self.frame:IsVisible())then
		getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetColor(unpackCol(self:Profile().PROGRESSBAR.FAIL_COLOR));
		self:SetState(nil, nil, nil, nil, true, true, 1);
    elseif(event == "CASTING_DELAYED" and self.frame:IsVisible())then
		self.stopTime = self.StopTime + arg1;
    end;
end;

function ikarus.delegates.CastBarUnitDelegateClass:OnScriptEvent(event, frame, key, ...)
	if(event == "OnUpdate") then
		if(self.spell) then
			self.spentTime = (GetTime() - self.startTime);
			self.progress = self.spentTime / (self.stopTime - self.startTime);
			if(self.progress > 1) then
				self.progress = 1;
			end;
			self:UpdateProgressBar();
			self:UpdateText();
		elseif(self.holdTime > GetTime()) then
			return;
		elseif(self.fadeOut) then
			self:UpdateProgressBar();
			self:UpdateText();
			if(self:Profile().GENERAL.HIDE_INACTIVE) then
				local alpha = self.frame:GetAlpha() - self.alphaStep;
				if(alpha > 0) then
					self.frame:SetAlpha(alpha);
				else
					getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetColor(unpackCol(self:Profile().PROGRESSBAR.FOREGROUND_COLOR));
					self.frame:Hide();
				end;
			else
				getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetColor(unpackCol(self:Profile().PROGRESSBAR.FOREGROUND_COLOR));
				self.fadeOut = false;
				self.progress = 0;
			end;
		end;
	else
		self:SuperClass():OnScriptEvent(self, event, frame, key, ...);
	end;
end;

function ikarus.delegates.CastBarUnitDelegateClass:HideDefaultFrame()
	CastingBarFrame:UnregisterEvent("CASTING_START");
	CastingBarFrame:UnregisterEvent("CASTING_STOP");
	CastingBarFrame:UnregisterEvent("CASTING_FAILED");
	CastingBarFrame:UnregisterEvent("CASTING_DELAYED");
	CastingBarFrame:Hide();
end;

function ikarus.delegates.CastBarUnitDelegateClass:ShowDefaultFrame()
	CastingBarFrame:RegisterEvent("CASTING_START");
	CastingBarFrame:RegisterEvent("CASTING_STOP");
	CastingBarFrame:RegisterEvent("CASTING_FAILED");
	CastingBarFrame:RegisterEvent("CASTING_DELAYED");
	-- Bad, don't show, let RoM script do it
	--CastingBarFrame:Show();
end;

function ikarus.delegates.CastBarUnitDelegateClass:UpdateText()
	if(self:Profile().GENERAL.VISIBLE) then
		--  Bar
		self:UpdateBarText(self, "PROGRESSBAR");
		
		--  Labels
		local config = self:Profile();
		self:UpdateLabel(self, config.LABELS, "TOPTEXT");
		self:UpdateLabel(self, config.LABELS, "RIGHTTEXT");
		self:UpdateLabel(self, config.LABELS, "BOTTOMTEXT");
		self:UpdateLabel(self, config.LABELS, "LEFTTEXT");
	end;
end;

function ikarus.delegates.CastBarUnitDelegateClass:UpdateAll()
	if(self:Profile().GENERAL.VISIBLE) then
		if(self:IsDummy()) then
			local config = self:Profile();
			self:ShowDummyBar(config.PROGRESSBAR);
			self:ShowDummyLabel(config.LABELS, "LEFTTEXT");
			self:ShowDummyLabel(config.LABELS, "RIGHTTEXT");
			self:ShowDummyLabel(config.LABELS, "TOPTEXT");
			self:ShowDummyLabel(config.LABELS, "BOTTOMTEXT");
			self.frame:Show();
		else
			self:UpdateProgressBar();
			self:UpdateText();
			if not(self:Profile().GENERAL.HIDE_INACTIVE) then
				self.frame:Show();
			else
				self.frame:Hide();
			end;
		end;
	else
		if(self.frame:IsVisible()) then
			self.frame:Hide();
		end;
	end;
end;

function ikarus.delegates.CastBarUnitDelegateClass:UpdateProgressBar()
	getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetWidth(self.frame:GetWidth() * self.progress);
	getglobal(self:Profile().PROGRESSBAR.FRAME.."ForegroundTexture"):SetTexCoord(0,self.progress,0,1);
end;

function ikarus.delegates.CastBarUnitDelegateClass:LoadConfig() 
	local config = self:Profile();
	
	--  Hide default frame or show it
	if(not config.GENERAL.VISIBLE and config.GENERAL.USE_DEFAULT_FRAME) then
		self:ShowDefaultFrame();
	else
		self:HideDefaultFrame();
	end;
		
	--  Bar
	self:LoadBarConfig(config.PROGRESSBAR);
	
	--  Labels
	--[[local font = tostring(ikarus.constants.ADDON_DIR .. "fonts/")
	if(config.LABELS.TOPTEXT_VISIBLE) then
		getglobal(config.LABELS.TOPTEXT_FRAME):SetFont(font..tostring(config.LABELS.TOPTEXT_FONT));
	end;
	if(config.LABELS.RIGHTTEXT_VISIBLE) then
		getglobal(config.LABELS.RIGHTTEXT_FRAME):SetFont(font..tostring(config.LABELS.RIGHTTEXT_FONT));
	end;
	if(config.LABELS.BOTTOMTEXT_VISIBLE) then
		getglobal(config.LABELS.BOTTOMTEXT_FRAME):SetFont(font..tostring(config.LABELS.BOTTOMTEXT_FONT));
	end;
	if(config.LABELS.LEFTTEXT_VISIBLE) then
		getglobal(config.LABELS.LEFTTEXT_FRAME):SetFont(font..tostring(config.LABELS.LEFTTEXT_FONT));
	end;
	]]
	
	self:LoadBoundProperties();
end;

function ikarus.delegates.CastBarUnitDelegateClass:LoadBoundProperties()
	local config = self:Profile();
	
	if (config.GENERAL.VISIBLE) then
		local scale = GetUIScale() * tonumber(config.GENERAL.SCALE);
		
		local frame = self.frame;
		frame.mainUnit = self:Unit();
		frame.unit = self:SubUnit();
		local height, width = tonumber(config.GENERAL.HEIGHT), tonumber(config.GENERAL.WIDTH);
		
		if(config.DOCKING and config.DOCKING.ENABLE) then
			local target = ikarus.profile[string.lower(config.DOCKING.TARGET)].GENERAL.FRAME;
			ikarus.profile[self.unit].GENERAL.FRAME_LOCKED = true;
			ikarus.ui:SetAlignment(frame, target, config.DOCKING.ALIGNMENT, config.DOCKING.OFFSET_X, config.DOCKING.OFFSET_Y, GetUIScale());
		elseif not(config.GENERAL.PROXY == true) then
			ikarus.ui:SetAnchor(frame, "UIParent", "TOPLEFT", "TOPLEFT", config.GENERAL.OFFSET_X, config.GENERAL.OFFSET_Y, GetUIScale());
		end;
		ikarus.ui:Resize(frame, width, height, scale);
		frame:SetAlpha(tonumber(config.GENERAL.ALPHA));	
				
		--  Progress Bar
		self:LoadBarBoundProperties(config.PROGRESSBAR, self, width, height, scale);
		
		--  Labels
		self:LoadTextBoundProperties(config.LABELS, frame, "TOPTEXT", width, 25, scale);
		ikarus.ui:SetAnchor(config.LABELS.TOPTEXT_FRAME, frame, "BOTTOMLEFT", "TOPLEFT", config.LABELS.TOPTEXT_OFFSET_X, config.LABELS.TOPTEXT_OFFSET_Y);
		
		self:LoadTextBoundProperties(config.LABELS, frame, "RIGHTTEXT", width, 25, scale);
		ikarus.ui:SetAnchor(config.LABELS.RIGHTTEXT_FRAME, frame, "TOPLEFT", "TOPRIGHT", config.LABELS.RIGHTTEXT_OFFSET_X, config.LABELS.RIGHTTEXT_OFFSET_Y);
		
		self:LoadTextBoundProperties(config.LABELS, frame, "BOTTOMTEXT", width, 25, scale);
		ikarus.ui:SetAnchor(config.LABELS.BOTTOMTEXT_FRAME, frame, "TOPLEFT", "BOTTOMLEFT", config.LABELS.BOTTOMTEXT_OFFSET_X, config.LABELS.BOTTOMTEXT_OFFSET_Y);
		
		self:LoadTextBoundProperties(config.LABELS, frame, "LEFTTEXT", width, 25, scale);
		ikarus.ui:SetAnchor(config.LABELS.LEFTTEXT_FRAME, frame, "TOPRIGHT", "TOPLEFT", config.LABELS.LEFTTEXT_OFFSET_X, config.LABELS.LEFTTEXT_OFFSET_Y);
		
		self:SetState(nil, nil, nil, nil, false, false, 0);			
		self:UpdateAll();
	else
		getglobal(config.GENERAL.FRAME):Hide();
	end;
end;
--  ***********************************************************************

--  Instantiate our delegate 
return ikarus.delegates.CastBarUnitDelegateClass:Create();
	
end;